/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var index = require('./routes/index');
var user = require('./routes/user');
var register = require('./routes/register');
var http = require('http');
var path = require('path');
var file = require('./routes/file');
var list = require('./routes/list');
var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({
    uploadDir: __dirname + '/public/temp'
}));
app.use(express.limit('5mb'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));

// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}

// 首页
app.get('/', routes.index);
app.post('/check', index.checkLogin);

app.get('/users', user.list);

// 注册页面
app.get('/register', register.register);
app.post('/checkUserName', register.checkUserName);
app.post('/submitRegister', register.submitRegister);

app.get('/file', file.index);
app.get('/file/index', file.index);
app.all('/file/uploader', file.uploader);
app.get("/list",list.list);
app.get("/fileList",list.getFileList);
app.get('/download', function(req, res){
    var file = decodeURIComponent( __dirname +'/public/'+ req.query.file);
    var fileName = decodeURIComponent(req.query.fileName);
    console.log(""); console.log("");
    console.log("fileNmae" +fileName);
    console.log("");
    console.log("");
    res.charset = 'utf-8';
    res.download(file,fileName); // Set disposition and send it.
});

http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});